package com.ez.supervise.test.base;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public abstract class AbstractSpringTestCase extends TestCase {

    private static ApplicationContext context;
    private TransactionStatus transactionStatus;
    private PlatformTransactionManager transactionManager;
    private boolean defaultRollback = false;
    static {
	context = new ClassPathXmlApplicationContext("spring/spring-supervise-test.xml");
    }

    public AbstractSpringTestCase() {

    }

    public AbstractSpringTestCase(String name) {
	super(name);
    }

    @Override
    protected void setUp() throws Exception {
	transactionManager = (PlatformTransactionManager) getBean("transactionManager");
	transactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
	super.setUp();
    }

    /*
     * (non-Javadoc)
     * 
     * @see junit.framework.TestCase#tearDown()
     */
    @Override
    protected void tearDown() throws Exception {
	if (defaultRollback)
	    transactionManager.rollback(this.transactionStatus);
	else
	    transactionManager.commit(this.transactionStatus);
	super.tearDown();
    }

    protected Object getBean(String beanName) {
	return context.getBean(beanName);
    }

}
